package com.gongpingjialoan.data;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CategoryData implements Parcelable {

    public List<Map<String, String>> mBrandList;

    public List<Map<String, String>> mHotBrandList;

    public List<Map<String, String>> mModelList;

    public Map<String, List<Map<String, String>>> mBrandModelMap;

    public Map<String, Bitmap> mBrandLogoImg;

    public Map<String, Bitmap> mModelLogoImg;

    public CategoryData() {
        mBrandList = new ArrayList<Map<String, String>>();
        mModelList = new ArrayList<Map<String, String>>();
        mHotBrandList = new ArrayList<Map<String, String>>();
        mBrandModelMap = new HashMap<String, List<Map<String, String>>>();

        mBrandLogoImg = new HashMap<String, Bitmap>();
        mModelLogoImg = new HashMap<String, Bitmap>();
    }

    public void LoadBrandData(JSONArray json) {

        try {
            HashMap<String, String> ulmtdBrandMap = new HashMap<String, String>();
            ulmtdBrandMap.put("name", "不限品牌");
            ulmtdBrandMap.put("slug", "ulimtd_slug");
            ulmtdBrandMap.put("first_letter", "热门品牌");
            ulmtdBrandMap.put("logo_img", "localimage");

            mBrandList.add(ulmtdBrandMap);
            for (int i = 0; i < json.length(); i++) {
                JSONObject brandItem = json.getJSONObject(i);

                HashMap<String, String> brandMap = new HashMap<String, String>();
                brandMap.put("name", brandItem.getString("name"));
                brandMap.put("slug", brandItem.getString("slug"));
                brandMap.put("first_letter", brandItem.getString("first_letter"));
                brandMap.put("logo_img", brandItem.getString("logo_img"));
                brandMap.put("max_price_bn", brandItem.getString("max_price_bn"));
                mBrandList.add(brandMap);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    public String getLogoByBrand(String brand) {
        String url = "";
        for (int i = 0; i < mBrandList.size(); i++) {
            if (mBrandList.get(i).get("slug").equals(brand)) {
                url = mBrandList.get(i).get("logo_img");
            }
        }
        return url;
    }

    public int getBrandPosition(String brandslug) {
        int position = -1;
        for (int i = 0; i < mBrandList.size(); i++) {
            if (mBrandList.get(i).get("slug").equals(brandslug)) {
                position = i;
            }
        }

        return position;
    }

    public void LoadHotBrandData(JSONArray json) {
        if (json != null && json.length() > 0 && mHotBrandList != null)
            mHotBrandList.clear();
        try {
            for (int i = 0; i < json.length(); i++) {
                JSONObject brandItem = json.getJSONObject(i);
                HashMap<String, String> brandMap = new HashMap<String, String>();
                brandMap.put("name", brandItem.getString("name"));
                brandMap.put("slug", brandItem.getString("slug"));
                brandMap.put("logo_img", brandItem.getString("logo_img"));
                mHotBrandList.add(brandMap);
            }
        } catch (Exception e) {
        }
        EventBus.getDefault().post("hotCarloaded");
    }

    public String getLogoByModer(String model) {
        String url = "";
        for (int i = 0; i < mModelList.size(); i++) {
            if (mModelList.get(i).get("slug").equals(model)) {
                url = mModelList.get(i).get("logo_img");
            }
        }
        return url;
    }

    public void LoadModelData(JSONArray json) {

        try {

            for (int i = 0; i < json.length(); i++) {
                JSONObject modelItem = json.getJSONObject(i);

                HashMap<String, String> modelMap = new HashMap<String, String>();
                modelMap.put("name", modelItem.getString("name"));
                modelMap.put("slug", modelItem.getString("slug"));
                modelMap.put("parent", modelItem.getString("parent"));
                modelMap.put("logo_img", modelItem.getString("logo_img"));
                modelMap.put("thumbnail", modelItem.getString("thumbnail"));
                modelMap.put("keywords", modelItem.getString("keywords"));
                mModelList.add(modelMap);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeList(mBrandList);
        parcel.writeList(mHotBrandList);
        parcel.writeMap(mBrandModelMap);
    }

    public List<Map<String, String>> getModelsByBrand(String brand) {
        List<Map<String, String>> modelsUnderBrand = new LinkedList<Map<String, String>>();

        HashMap<String, String> modelMap = new HashMap<String, String>();
        modelMap.put("name", "不限车型");
        modelMap.put("slug", "ulimtd_slug");
        modelMap.put("logo_img", "localimage");
        modelsUnderBrand.add(modelMap);
        for (int i = 0; i < mModelList.size(); i++) {
            if (mModelList.get(i).get("parent").equals(brand)) {
                modelsUnderBrand.add(mModelList.get(i));
            }
        }

        return modelsUnderBrand;
    }

    public class SearchResult implements Serializable {
        public String mBrandName;

        public String mBrandSlug;

        public String mModelName;

        public String mModelSlug;

        public int nums;

        public String mModelThumbnail;

        public boolean mIsBrand;

        public SearchResult() {
            mBrandName = null;
            mBrandSlug = null;
            mModelName = null;
            mModelSlug = null;
            mModelThumbnail = null;
            mIsBrand = false;
        }
    }

    public boolean isContainChinese(String str) {

        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    public List<SearchResult> search(String keywords, JSONObject data) {

        if (isContainChinese(keywords)) {
            keywords = keywords.toUpperCase();
        }
        List<SearchResult> results = new ArrayList<SearchResult>();
        Iterator<Map<String, String>> iter = mModelList.iterator();
        while (iter.hasNext()) {
            Map<String, String> key = iter.next();
            if (key.get("keywords").contains(keywords) || key.get("name").contains(keywords)
                    || key.get("slug").contains(keywords)) {
                SearchResult rlt = new SearchResult();
                if (null != key.get("parent") && !key.get("parent").equals("null")) {
                    rlt.mIsBrand = false;
                    rlt.mBrandSlug = key.get("parent");
                    rlt.mModelSlug = key.get("slug");
                    rlt.mModelName = key.get("name");
                    rlt.mModelThumbnail = key.get("thumbnail");
                    if (data != null) {
                        try {
                            JSONObject j1 = data.getJSONObject(rlt.mBrandSlug);
                            JSONObject j2 = j1.getJSONObject("models");
                            rlt.nums = j2.getInt(rlt.mModelSlug);
                        } catch (JSONException e) {
                            rlt.nums = 0;
                        }
                    }
                    Iterator<Map<String, String>> brandIter = mBrandList.iterator();
                    while (brandIter.hasNext()) {
                        Map<String, String> brand = brandIter.next();
                        if (brand.get("slug").equals(rlt.mBrandSlug)) {
                            rlt.mBrandName = brand.get("name");
                            break;
                        }
                    }
                } else {
                    rlt.mIsBrand = true;
                    rlt.mBrandSlug = key.get("slug");
                    rlt.mBrandName = key.get("name");
                    if (data != null) {
                        try {
                            JSONObject j1 = data.getJSONObject(rlt.mBrandSlug);
                            rlt.nums = j1.getInt("total");
                        } catch (JSONException e) {
                            rlt.nums = 0;
                        }
                    }
                }
                results.add(rlt);
            }
        }

        return results;
    }


    public List<SearchResult> addAllModel() {

        List<SearchResult> results = new ArrayList<SearchResult>();
        Iterator<Map<String, String>> iter = mModelList.iterator();

        while (iter.hasNext()) {
            Map<String, String> key = iter.next();
            SearchResult rlt = new SearchResult();
            if (null != key.get("parent") && !key.get("parent").equals("null")) {
                rlt.mIsBrand = false;
                rlt.mBrandSlug = key.get("parent");
                rlt.mModelSlug = key.get("slug");
                rlt.mModelName = key.get("name");
                rlt.mModelThumbnail = key.get("thumbnail");

                Iterator<Map<String, String>> brandIter = mBrandList.iterator();
                while (brandIter.hasNext()) {
                    Map<String, String> brand = brandIter.next();
                    if (brand.get("slug").equals(rlt.mBrandSlug)) {
                        rlt.mBrandName = brand.get("name");
                        break;
                    }
                }
                results.add(rlt);
            }
        }

        return results;
    }

    public List<SearchResult> autocomplete(String keywords) {

        List<SearchResult> results = new ArrayList<SearchResult>();
        Iterator<Map<String, String>> iter = mModelList.iterator();

        int count = 0;
        int max = 10;

        while (iter.hasNext()) {
            Map<String, String> key = iter.next();
            if (key.get("name").startsWith(keywords) || key.get("keywords").startsWith(keywords)
                    || key.get("slug").startsWith(keywords)) {
                SearchResult rlt = new SearchResult();
                if (null != key.get("parent") && !key.get("parent").equals("null")) {
                    rlt.mIsBrand = false;
                    rlt.mBrandSlug = key.get("parent");
                    rlt.mModelSlug = key.get("slug");
                    rlt.mModelName = key.get("name");

                    Iterator<Map<String, String>> brandIter = mBrandList.iterator();
                    while (brandIter.hasNext()) {
                        Map<String, String> brand = brandIter.next();
                        if (brand.get("slug").equals(rlt.mBrandSlug)) {
                            rlt.mBrandName = brand.get("name");
                            break;
                        }
                    }
                } else {
                    rlt.mIsBrand = true;
                    rlt.mBrandSlug = key.get("slug");
                    rlt.mBrandName = key.get("name");
                }
                results.add(rlt);
                if ((++count) >= max)
                    break;
            }
        }

        return results;
    }
}
